package com.ljy.my_study.leetcode.区域和检索之数组不可变;

/**
 * @author lijunying
 * @date 2021/3/1 16:53
 */
public class NumArray {

    public static void main(String[] args) {
        int[] nums=new int[]{};
        NumArray numArray=new NumArray(nums);
        System.out.println(numArray.sumRange(0,0));
//        System.out.println(numArray.sumRange(2,5));
//        System.out.println(numArray.sumRange(0,5));
    }

    private int[] sums;
    private int len;

    public NumArray(int[] nums) {
        len=nums.length;
        if(len==0){
            return;
        }
        sums=new int[len];
        int sum=0;
        sums[0]=nums[0];
        for(int i=1;i<len;i++){
            sums[i]=sums[i-1]+nums[i];
        }
    }

    public int sumRange(int i, int j) {
        if(len==0){
            return 0;
        }
        if(i==0){
            return sums[j];
        }
        return sums[j]-sums[i-1];
    }
}
